var bauto = true;
function OnSetAuto(auto){
	bauto = auto;
};

function OnPlay(isPlay) {
	if(bauto){
		var video = document.querySelector("video");
		if(isPlay) {
			video.play();
		} else {
			video.pause();
		}	
	}
}



(function() {
	var Blur = function() {
		document.querySelector("input[type='text']").blur();
		document.querySelector(".item_password").querySelector("input").blur();
	}
	
	mui.init();

	mui.plusReady(function() {
		//还原单选框
		require(["storage"], function(storage) {
			var login_keep_pwd = storage.getItem("login_keep_pwd")
			var login_auto_login = storage.getItem("login_auto_login")
		
			if(login_keep_pwd == true) {
				document.getElementById("login_keep_pwd").classList.remove("active");
				var user_info = storage.getItem("login_user_info");
				if(user_info != null){
					document.querySelector("input[type='text']").value = user_info["username"];
					document.querySelector(".item_password").querySelector("input").value = user_info["pwd"];
				}
			} else {
				document.getElementById("login_keep_pwd").classList.add("active");
			}

			if(login_auto_login == true) {
				document.getElementById("login_auto_login").classList.remove("active");
			} else {
				document.getElementById("login_auto_login").classList.add("active");
			}
		})
		
		//--------------end

		//单选样式切换
		var radio = document.querySelector(".from>.radio");
		mui(".from").on("tap", ".radio", function(e) {
			Blur();
			if(!e.target.classList.contains("radio")) {
				require(["storage"], function(storage) {
					id = e.target.getAttribute("id");
					console.log(id)
					if(e.target.classList.contains("active")) {
						storage.setItem(id, true);
						e.target.classList.remove("active");
					} else {
						storage.setItem(id, false);
						e.target.classList.add("active");
					}
				})
			}
		}, false)

		//登陆
		mui(".login").on("tap", "button", function() {
			Blur();
			var username = document.querySelector("input[type='text']").value;
			var pwd = document.querySelector(".item_password").querySelector("input").value;
			if(username.length ==0 || pwd.length == 0){
				mui.alert("请正确输入账号密码")
				return;
			}
			
			var md5_pwd = md5(pwd).toUpperCase();
			require(["login","storage","getui"], function(login,storage,getui) {
				login.OnLogin(username,md5_pwd,function(data){
					//判断是否记住密码
					var login_keep_pwd = storage.getItem("login_keep_pwd")
					if(login_keep_pwd == true) {
						storage.setItem("login_user_info", {username:username,pwd:pwd});
					}else{
						storage.setItem("login_user_info", null);
					}
					//--------------------------end
					getui.OnBindUser();//绑定user
					
					if(data.is_bind_phone == true){
						console.log("登录成功")
						OnOpenWindow("../main.html","main.html")
						OnDestroy(null);	
					}else{//绑定手机号
						console.log("需要绑定手机号页面")
						OnOpenWindow("../bind/bind_phone.html","bind_phone.html",{},false)
						OnDestroy(null);
						OnPlay(false);
					}
				});
			})
		})

		//显示密码
		var from_icon_end = document.querySelector(".from_icon_end");
		from_icon_end.addEventListener("tap", function() {
			var input = this.parentNode.querySelector("input");
			if(this.classList.contains("active")) {
				this.classList.remove("active");
				this.querySelector("use").setAttribute("xlink:href", "#icon-eyeshover");
				input.setAttribute("type", "password");
			} else {
				this.classList.add("active");
				this.querySelector("use").setAttribute("xlink:href", "#icon-eyes")
				input.setAttribute("type", "text")
			}
		}, false);

		//忘记密码
		mui(".register").on("tap","span",function(){
			OnOpenWindow("../login/getpwdby_phone.html","getpwdby_phone.html")
			OnPlay(false);
			OnSetAuto(false);
		})
		
		//后台切换到前台
		document.addEventListener("resume", function() {
			OnPlay(true);
		}, false);
		
	})

})()
